package com.mark.web.data.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.mark.common.ServerResponse;
import com.mark.web.data.mapper.HeadLineMapper;
import com.mark.web.data.exception.HeadLineException;
import com.mark.web.data.entity.po.HeadLine;
import com.mark.web.data.service.IHeadLineService;
import com.mark.web.data.util.JsonUtil;
import com.mark.web.data.entity.vo.HeadLineVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 公告信息业务逻辑实现
 * @Author: Mark
 * @CreateDate: 2018/8/14.
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Slf4j
@Service("headLineServiceImpl")
public class HeadLineServiceImpl implements IHeadLineService {
    @Autowired
    private HeadLineMapper headLineMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    //1.新增或者更新操作
    @Override
    public ServerResponse insertOrUpdate(HeadLine headLine) {
        try {
            if (headLine.getLineId() == null) {//如果对象中含有ID则为更新否则为新增
                headLine.setEnableStatus(1);
                headLine.setLineImg(headLine.getLineLink());
                int rowCount = headLineMapper.insert(headLine);
                if (rowCount >= 0) {
                    String redisToken = "headLines";
                    redisTemplate.delete(redisToken);
                    //RedisShardedPoolUtil.del(redisToken);//如果更新操作将redis缓存的信息删除
                    return ServerResponse.createBySuccess("发布公告信息成功");
                }
                return ServerResponse.createByErrorMessage("发布公告信息失败！");
            } else {
                int rowCount = headLineMapper.updateByPrimaryKeySelective(headLine);
                if (rowCount >= 0) {
                    String redisToken = "headLines";
                    redisTemplate.delete(redisToken);
                    //RedisShardedPoolUtil.del(redisToken);//如果更新操作将redis缓存的信息删除
                    return ServerResponse.createBySuccess("更新公告信息成功");
                }
                return ServerResponse.createByErrorMessage("更新公告信息失败！");
            }
        }catch (Exception e){
            throw new HeadLineException("insertOrUpdate",e.getClass()+e.getMessage());
        }
    }

    //2.获取公告信息列表
    @Override
    public ServerResponse getHeadLineList(int pageNum, int pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<HeadLine> headLineList = headLineMapper.selectList();
            List<HeadLineVo> headLineVoList = Lists.newArrayList();
            for (HeadLine headLine : headLineList) {
                HeadLineVo headLineVo = new HeadLineVo();
                headLineVo = headLineVo.assembleHeadLineVo(headLine);
                headLineVoList.add(headLineVo);
            }
            PageInfo pageResult = new PageInfo(headLineList);
            pageResult.setList(headLineVoList);
            return ServerResponse.createBySuccess(pageResult);
        }catch (Exception e){
            throw new HeadLineException("getHeadLineList",e.getClass()+e.getMessage());
        }
    }

    //------------------------ portal operation--------------------------------
    //前端获取按照权重值排序的前三条公告信息数据
    @Override
    public ServerResponse portalGetHeadLineList(int pageNum, int pageSize) {
        try {
            List<HeadLine> headLineList = Lists.newArrayList();
            String redisToken = "headLines";
            //String HeadLinesJsonStr = RedisShardedPoolUtil.get(redisToken);//从Redis取值
            String HeadLinesJsonStr = redisTemplate.opsForValue().get(redisToken);
            if (HeadLinesJsonStr != null) {
                headLineList = JsonUtil.string2Obj(HeadLinesJsonStr, List.class, HeadLine.class);
                log.info("正在从Redis缓存中取【Headlines】数据");
            } else {
                PageHelper.startPage(pageNum, pageSize);
                headLineList = headLineMapper.portalGetHeadLineList();
                //RedisShardedPoolUtil.set(redisToken, JsonUtil.obj2String(headLineList));//缓存到Redis
                redisTemplate.opsForValue().set(redisToken, JsonUtil.obj2String(headLineList));
            }
            List<HeadLineVo> headLineVoList = Lists.newArrayList();
            for (HeadLine headLine : headLineList) {
                HeadLineVo headLineVo = new HeadLineVo();
                headLineVo = headLineVo.assembleHeadLineVo(headLine);
                headLineVoList.add(headLineVo);
            }
            PageInfo pageResult = new PageInfo(headLineList);
            pageResult.setList(headLineVoList);
            return ServerResponse.createBySuccess(pageResult);
        }catch (Exception e){
            throw new HeadLineException("portalGetHeadLineList",e.getClass()+e.getMessage());
        }
    }
}
